/****
 *
 * $Id: util.c,v 1.5 2009/09/29 23:16:58 rdilley Exp $
 *
 * Utility functions
 *
 * Copyright (c) 2009, Ron Dilley
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   - Neither the name of Uberadmin/BaraCUDA/Nightingale nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ****/

/****
 *
 * includes
 *
 ****/

#include "util.h"
#include <stdarg.h>


/****
 *
 * local variables
 *
 ****/

PRIVATE char *cvsid = "$Id: util.c,v 1.5 2009/09/29 23:16:58 rdilley Exp $";

/****
 *
 * functions
 *
 ****/



/*!
 * exit facility with an error message
 * @param  err format string
 * @param  ...  arg list
 * @return void
 * \version 1.0
 * \date   Sep 2008
 * \author Elie
 */
void die(char *err, ...)
{
	va_list ap;
	va_start(ap, err);
	fflush(stdout);
	vfprintf(stderr, err, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
	/* XXX should shutdown cuda gracefully */
}

/*!
 * Warning facility
 * @param  err format string
 * @param  ...  arg list
 * @return void
 * \version 1.0
 * \date   Sep 2008
 * \author Elie
 */
void warning(char *err, ...)
{
	//no warning if in quiet mode
	//if(option.quiet)
	//	return;
	va_list ap;
	va_start(ap, err);
	//if(option.errout)
	//	vfprintf(option.errout, err, ap);
	//else
		vfprintf(stderr, err, ap);
	va_end(ap);
	//exit(EXIT_FAILURE);
}


/****
 *
 * convert hash to hex
 *
 ****/

char *hash2hex(const char *hash, char *hashStr, int hLen ) {
  int i;
  char hByte[3];
  bzero( hByte, sizeof( hByte ) );

  for( i = 0; i < hLen; i++ ) {
    sprintf( hByte, "%02x", hash[i] & 0xff );
    sprintf( hashStr+(i*2), hByte );
  }

  return hashStr;
}

/****
 *
 * high performance memcmp (byte)
 *
 ****/

int bytecmp( const void *a, const void *b, size_t n ) {
  uint8_t *ua = (uint8_t *)a;
  uint8_t *ub = (uint8_t *)b;
  uint8_t *ua_max = ua + n;

  while ( ua < ua_max ) {
    if ( *ua EQ *ub ) {
      ua++;
      ub++;
    } else
      return 1;
  }
  return 0;
}

/****
 *
 * high performance memcmp (word)
 *
 ****/

int wordcmp( const void *a, const void *b, size_t n ) {
  uint16_t num_words = n / sizeof( uint16_t );
  uint16_t extra_bytes = n % sizeof( uint16_t );
  uint16_t *ua = (uint16_t *)a;
  uint16_t *ub = (uint16_t *)b;
  uint16_t *ua_max = ua + num_words;
  uint8_t *uac;
  uint8_t *ubc;
  uint8_t *uac_max;

  while( ua < ua_max ) {
    if ( *ua EQ *ub ) {
      ua++;
      ub++;
    } else
      return 1;
  }
  uac = (uint8_t *)ua;
  ubc = (uint8_t *)ub;
  uac_max = uac + extra_bytes;
  while( uac < uac_max ) {
    if ( *uac EQ *ubc ) {
      uac++;
      ubc++;
    } else
      return 1;
  }
  return 0;
}

/****
 *
 * high performance memcmp (word)
 *
 ****/

int dwordcmp( const void *a, const void *b, size_t n ) {
  uint32_t num_dwords = n / sizeof( uint32_t );
  uint32_t extra_bytes = n % sizeof( uint32_t );
  uint32_t *ua = (uint32_t *)a;
  uint32_t *ub = (uint32_t *)b;
  uint32_t *ua_max = ua + num_dwords;
  uint8_t *uac;
  uint8_t *ubc;
  uint8_t *uac_max;

  while( ua < ua_max ) {
    if ( *ua EQ *ub ) {
      ua++;
      ub++;
    } else
      return 1;
  }
  uac = (uint8_t *)ua;
  ubc = (uint8_t *)ub;
  uac_max = uac + extra_bytes;
  while( uac < uac_max ) {
    if ( *uac EQ *ubc ) {
      uac++;
      ubc++;
    } else
      return 1;
  }
  return 0;
}
